# Copyright (C) 2006  Apptility LLC  http://www.apptility.com
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

class FeedNotify < ActionMailer::Base
  #require 'rubygems'
  #require_gem 'feedtools'
  require 'feed_tools' 
  
  def emailfeed(user, useremail,feedu)
    setup_email(user, useremail)

    # Email header info
    # @subject += "Welcome to #{LoginEngine.config(:app_name)}!"
    @subject += "Feed notification by Ajuby open source application builder"
    part :content_type => "text/html"
    
    # Email body substitutions
    bodyString = get_feed_body(feedu)
    print bodyString
    @body = bodyString
    
 
  end

  def get_feed_body(feedu)
     bodystring =""
     if (feedu!=nil)
        bodystring = bodystring + "<table style='text-align: left; background-color: rgb(51, 0, 153); width: 933px; height: 50px; padding='20px' border='0' cellpadding='2' cellspacing='2'><tbody><tr><td style='width: 100%;'<span style='color: rgb(255, 255, 255);'><H3>&nbsp;"+feedu.title+"</H3> </span></td></tr><tr><td style='width: 100%;'><span style='color: rgb(255, 255, 255);'><H4>&nbsp;"+feedu.description+"</H4></span></td></tr></tbody></table>"
        for feed_item in feedu.items
          bodystring = bodystring + "<table style='text-align: left; background-color: rgb(255, 204, 102); width: 933px; height: 50px;' padding='20px' border='0' cellpadding='2' cellspacing='2'><tbody><tr><td style='width: 100%; background-color: rgb(102, 51, 0);'><span><a href='"+feed_item.link+"'>"+feed_item.title+"</a></span></td></tr><tr><td style='width: 100%;'><span>&nbsp;"+feed_item.description+"</span></td></tr></tbody></table><hr>"
         end 
     end
     bodystring = bodystring + "."
 end
  
  
  def setup_email(user, useremail)
    @recipients = useremail
    @from       = LoginEngine.config(:email_from).to_s
    @subject    = "[#{LoginEngine.config(:app_name)}] "
    @sent_on    = Time.now
    @headers['Content-Type'] = "text/html; charset=#{LoginEngine.config(:mail_charset)}; format=flowed"
  end
  
    def current_theme
      #theme = "googlepagesx" 
      #self.class.read_inheritable_attribute("theme")
    end 
 
    def force_liquid_template
      #theme = "googlepagesx" 
      #self.class.read_inheritable_attribute("theme")
    end 
    
end
